﻿using System;
using System.Collections.Generic;
using Engine;
using Engine.Graphics;
using Game;
using GameEntitySystem;
namespace Mekiasm
{
    public class ThrowPickable : Pickable
    {
        public ComponentBody componentBody;
        public ComponentBody FishBody;
        public ComponentHumanModel componentHumanModel;
        public ComponentPathfinding ComponentPathfinding;
        public bool AllowToRemove = false;
        public bool GSplashGenerated;
        public bool Flag = false;
        public bool IsFinish = false;
        public bool injured = false;
        public double? StartEatTime;
        public int ActiveValue = 0;
        public Vector3 RunawayPosition;
        public double NextTime = 0;
    }
    public class SubsystemThrowSystem
    {
        public SubsystemAudio m_subsystemAudio;

        public SubsystemPlayers m_subsystemPlayers;

        public SubsystemTerrain m_subsystemTerrain;

        public SubsystemSky m_subsystemSky;

        public SubsystemTime m_subsystemTime;

        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemParticles m_subsystemParticles;

        public SubsystemExplosions m_subsystemExplosions;

        public SubsystemBlockBehaviors m_subsystemBlockBehaviors;

        public SubsystemFireBlockBehavior m_subsystemFireBlockBehavior;

        public SubsystemFluidBlockBehavior m_subsystemFluidBlockBehavior;
        public SubsystemBodies m_subsystemBodies;

        public List<ThrowPickable> m_pickables = new List<ThrowPickable>();

        public List<ThrowPickable> m_pickablesToRemove = new List<ThrowPickable>();

        public PrimitivesRenderer3D m_primitivesRenderer = new PrimitivesRenderer3D();

        public Game.Random m_random = new Game.Random();

        public DrawBlockEnvironmentData m_drawBlockEnvironmentData = new DrawBlockEnvironmentData();

        public ReadOnlyList<ThrowPickable> Pickables => new ReadOnlyList<ThrowPickable>(m_pickables);

        public Action<ThrowPickable> PickableRemoved;

        public ThrowPickable AddPickable(ComponentBody componentBody, int Pickable, int value, Vector3? velocity, Matrix? stuckMatrix)
        {
            ThrowPickable pickable = new ThrowPickable();
            pickable.Value = value;
            pickable.componentBody = componentBody;
            pickable.componentHumanModel = componentBody.Entity.FindComponent<ComponentHumanModel>();
            pickable.Position = GetStartPosition(componentBody);
            pickable.StuckMatrix = stuckMatrix;
            pickable.ActiveValue = Pickable;
            pickable.CreationTime = m_subsystemGameInfo.TotalElapsedGameTime;
            if (velocity.HasValue)
            {
                pickable.Velocity = velocity.Value;
            }
            else
            {
                pickable.Velocity = new Vector3(m_random.Float(-0.5f, 0.5f), m_random.Float(1f, 1.2f), m_random.Float(-0.5f, 0.5f));
            }
            m_pickables.Add(pickable);
            return pickable;
        }
        public Vector3 GetStartPosition(ComponentBody componentBody)
        {
            Vector3 pos = componentBody.Entity.FindComponent<ComponentHumanModel>().EyePosition + componentBody.Matrix.Right * 0.4f - componentBody.Matrix.Up * 0.5f + componentBody.Matrix.Forward * 0.4f; ;
            return pos;
        }


        public SubsystemThrowSystem(Project project)
        {

            m_subsystemAudio = project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_subsystemPlayers = project.FindSubsystem<SubsystemPlayers>(throwOnError: true);
            m_subsystemTerrain = project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_subsystemSky = project.FindSubsystem<SubsystemSky>(throwOnError: true);
            m_subsystemTime = project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemGameInfo = project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemParticles = project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_subsystemExplosions = project.FindSubsystem<SubsystemExplosions>(throwOnError: true);
            m_subsystemBlockBehaviors = project.FindSubsystem<SubsystemBlockBehaviors>(throwOnError: true);
            m_subsystemFireBlockBehavior = project.FindSubsystem<SubsystemFireBlockBehavior>(throwOnError: true);
            m_subsystemFluidBlockBehavior = project.FindSubsystem<SubsystemFluidBlockBehavior>(throwOnError: true);
            m_subsystemBodies = project.FindSubsystem<SubsystemBodies>();
        }
        public void Draw(Camera camera)
        {
            double totalElapsedGameTime = m_subsystemGameInfo.TotalElapsedGameTime;
            m_drawBlockEnvironmentData.SubsystemTerrain = m_subsystemTerrain;
            Matrix matrix = Matrix.Identity;
            float num = MathUtils.Min(m_subsystemSky.VisibilityRange, 30f);
            foreach (ThrowPickable pickable in m_pickables)
            {
                Vector3 position = pickable.Position;
                float num2 = Vector3.Dot(camera.ViewDirection, position - camera.ViewPosition);
                if (num2 > -0.5f && num2 < num)
                {
                    int num3 = Terrain.ExtractContents(pickable.Value);
                    Block block = BlocksManager.Blocks[num3];
                    Block block1 = BlocksManager.Blocks[Terrain.ExtractContents(pickable.ActiveValue)];
                    float num4 = 0f;
                    if (pickable.Flag)
                    {//进入水中浮动
                        num4 = (float)(totalElapsedGameTime - pickable.CreationTime);
                        if (!pickable.StuckMatrix.HasValue)
                        {
                            position.Y += 0.25f * MathUtils.Saturate(3f * num4);
                        }
                    }

                    int x = Terrain.ToCell(position.X);
                    int num5 = Terrain.ToCell(position.Y);
                    int z = Terrain.ToCell(position.Z);
                    TerrainChunk chunkAtCell = m_subsystemTerrain.Terrain.GetChunkAtCell(x, z);
                    if (chunkAtCell != null && chunkAtCell.State >= TerrainChunkState.InvalidVertices1 && num5 >= 0 && num5 < 255)
                    {
                        m_drawBlockEnvironmentData.Humidity = m_subsystemTerrain.Terrain.GetSeasonalHumidity(x, z);
                        m_drawBlockEnvironmentData.Temperature = m_subsystemTerrain.Terrain.GetSeasonalTemperature(x, z) + SubsystemWeather.GetTemperatureAdjustmentAtHeight(num5);
                        float f = MathUtils.Max(position.Y - (float)num5 - 0.75f, 0f) / 0.25f;
                        int num6 = pickable.Light = (int)MathUtils.Lerp(m_subsystemTerrain.Terrain.GetCellLightFast(x, num5, z), m_subsystemTerrain.Terrain.GetCellLightFast(x, num5 + 1, z), f);
                    }

                    m_drawBlockEnvironmentData.Light = pickable.Light;
                    m_drawBlockEnvironmentData.BillboardDirection = pickable.Position - camera.ViewPosition;
                    m_drawBlockEnvironmentData.InWorldMatrix.Translation = position;
                    if (pickable.StuckMatrix.HasValue)
                    {
                        Matrix matrix2 = pickable.StuckMatrix.Value;
                        block.DrawBlock(m_primitivesRenderer, pickable.Value, Color.White, 0.3f, ref matrix2, m_drawBlockEnvironmentData);
                        if (pickable.ActiveValue != 0) block1.DrawBlock(m_primitivesRenderer, pickable.ActiveValue, Color.White, 0.3f, ref matrix2, m_drawBlockEnvironmentData);
                    }
                    else
                    {
                        matrix.Translation = position + new Vector3(0f, 0.04f * MathUtils.Sin(3f * num4), 0f);
                        block.DrawBlock(m_primitivesRenderer, pickable.Value, Color.White, 0.3f, ref matrix, m_drawBlockEnvironmentData);
                        if (pickable.ActiveValue != 0) block1.DrawBlock(m_primitivesRenderer, pickable.ActiveValue, Color.White, 0.3f, ref matrix, m_drawBlockEnvironmentData);
                    }
                }
                DrawLines(camera, pickable);
            }
            m_primitivesRenderer.Flush(camera.ViewProjectionMatrix);
        }
        public void DrawLines(Camera camera, ThrowPickable pickable)
        {
            //从当前位置画线至开始位置
            PrimitivesRenderer3D primitivesRenderer3D = new PrimitivesRenderer3D();
            FlatBatch3D flatBatch3D = primitivesRenderer3D.FlatBatch();
            //计算手的位置
            Vector3 vector = pickable.componentHumanModel.EyePosition + pickable.componentBody.Matrix.Right * 0.4f - pickable.componentBody.Matrix.Up * 0.5f + pickable.componentBody.Matrix.Forward * 0.4f;
            Vector3.Transform(vector, camera.ViewProjectionMatrix);
            flatBatch3D.QueueLineStrip(GetPositions(vector, pickable.Position), Color.Black);
            primitivesRenderer3D.Flush(camera.ViewProjectionMatrix);
        }

        public List<Vector3> GetPositions(Vector3 start, Vector3 end)
        {
            List<Vector3> positions = new List<Vector3>();
            Vector3 vector = end - start;
            for (int i = 0; i < 60; i++)
            {
                positions.Add(start + vector * i / 60f);
            }
            return positions;
        }


        public void Update(float dt)
        {
            double totalElapsedGameTime = m_subsystemGameInfo.TotalElapsedGameTime;
            float num = MathUtils.Pow(0.5f, dt);
            float num2 = MathUtils.Pow(0.001f, dt);


            foreach (ThrowPickable pickable in m_pickables)
            {
                if (pickable.ToRemove && pickable.AllowToRemove)
                {
                    m_pickablesToRemove.Add(pickable);
                    continue;
                }
                Block block = BlocksManager.Blocks[Terrain.ExtractContents(pickable.Value)];
                TerrainChunk chunkAtCell = m_subsystemTerrain.Terrain.GetChunkAtCell(Terrain.ToCell(pickable.Position.X), Terrain.ToCell(pickable.Position.Z));
                if (chunkAtCell == null || chunkAtCell.State <= TerrainChunkState.InvalidContents4)
                {
                    continue;
                }

                Vector3 position = pickable.Position;
                Vector3 vector = position + pickable.Velocity * dt;
                if (!pickable.FlyToPosition.HasValue)
                {
                    pickable.ComponentPathfinding = null;
                    foreach (ComponentPlayer tmpPlayer in m_subsystemPlayers.ComponentPlayers)
                    {
                        ComponentBody componentBody = tmpPlayer.ComponentBody;
                        if (pickable.Velocity != Vector3.Zero)
                        {
                            BodyRaycastResult? bodyRaycast = tmpPlayer.ComponentMiner.Raycast<BodyRaycastResult>(new Ray3(pickable.Position, pickable.Velocity), RaycastMode.Interaction, false, true, false);
                            if (bodyRaycast.HasValue && Vector3.DistanceSquared(bodyRaycast.Value.ComponentBody.Position, pickable.Position) <= 1f && !pickable.injured)
                            {
                                pickable.injured = true;
                                Game.ComponentHealth componentHealth = bodyRaycast.Value.ComponentBody.Entity.FindComponent<Game.ComponentHealth>();
                                componentHealth.Injure(0.01f, tmpPlayer.ComponentMiner.ComponentCreature, true, "鱼钩");
                                pickable.Velocity = Vector3.Zero;
                            }

                        }
                        Vector3 v = componentBody.Position + new Vector3(0f, 0.75f, 0f);
                        float num6 = (v - pickable.Position).LengthSquared();
                        if (ILibrary.GetBasicValue(tmpPlayer.ComponentMiner.ActiveBlockValue) != FishingRod.Index)
                        {
                            pickable.FlyToPosition = GetStartPosition(componentBody);
                        }
                        if (num6 > 256f)
                        {
                            pickable.AllowToRemove = true;
                            pickable.ToRemove = true;
                        }
                    }
                }
                if (pickable.Flag && pickable.NextTime < ILibrary.SubsystemGameInfo.TotalElapsedGameTime)
                {
                    foreach (ComponentBody component in m_subsystemBodies.Bodies)
                    {
                        ComponentSwimAwayBehavior swimAwayBehavior = component.Entity.FindComponent<ComponentSwimAwayBehavior>();
                        if (swimAwayBehavior != null && pickable.ComponentPathfinding == null)
                        {
                            ComponentPathfinding pathfinding = component.Entity.FindComponent<ComponentPathfinding>();
                            Block block2 = BlocksManager.Blocks[Terrain.ExtractContents(pickable.ActiveValue)];
                            if (pathfinding != null)
                            {
                                float distans = Vector3.DistanceSquared(component.Position, pickable.Position);
                                if (distans > 1f && distans <= 300f)
                                {
                                    float pr = block2.GetNutritionalValue(pickable.ActiveValue);
                                    if (ILibrary.random.Bool(pr) && ILibrary.random.Bool(pr) && ILibrary.random.Bool(pr))
                                    {
                                        pathfinding.SetDestination(pickable.Position, 4f, 0.5f, 100, false, false, false, null);
                                        pickable.GSplashGenerated = false;
                                        pickable.ComponentPathfinding = pathfinding;
                                        pickable.FishBody = component;
                                    }
                                }
                                else if (Vector3.DistanceSquared(component.Position, pickable.Position) <= 1f)//进入范围
                                {
                                    if (!pickable.GSplashGenerated)
                                    {
                                        m_subsystemParticles.AddParticleSystem(new WaterSplashParticleSystem(m_subsystemTerrain, pickable.Position, large: false));
                                        m_subsystemAudio.PlayRandomSound("Audio/Splashes", 1f, m_random.Float(-0.2f, 0.2f), pickable.Position, 6f, autoDelay: true);
                                        pickable.GSplashGenerated = true;
                                    }
                                    if (!pickable.StartEatTime.HasValue) pickable.StartEatTime = ILibrary.SubsystemGameInfo.TotalElapsedGameTime;
                                    //添加水花特效
                                    if (pickable.FlyToPosition.HasValue && ILibrary.SubsystemGameInfo.TotalElapsedGameTime - pickable.StartEatTime <= 15f)
                                    {
                                        pickable.StartEatTime = null;
                                        component.Velocity = pickable.Velocity;
                                    }
                                }
                            }
                        }
                    }
                }
                if (pickable.ComponentPathfinding != null)
                {
                    pickable.ComponentPathfinding.SetDestination(pickable.Position, 2f, 0.5f, 100, false, false, false, null);
                    if (pickable.StartEatTime > 0 && ILibrary.SubsystemGameInfo.TotalElapsedGameTime - pickable.StartEatTime > 15f && !pickable.IsFinish)
                    {
                        ComponentMiner componentMiner = pickable.componentBody.Entity.FindComponent<ComponentMiner>();
                        if (SaveItemSystem.GetBlockData(componentMiner.ActiveBlockValue, out Common.SaveItem saveItem))
                        {
                            saveItem.RemoveSlotItems(0, 1);
                        }
                        pickable.NextTime = ILibrary.SubsystemGameInfo.TotalElapsedGameTime + 30;
                        pickable.IsFinish = true;
                        pickable.RunawayPosition = new Vector3(pickable.Position.X + ILibrary.random.Float(-60f, 60f), pickable.Position.Y - ILibrary.random.Float(0f, 10f), pickable.Position.Z + ILibrary.random.Float(-60f, 60f));
                    }
                    if (pickable.IsFinish)
                    {
                        if (Vector3.DistanceSquared(pickable.RunawayPosition, pickable.Position) > 10f) pickable.ComponentPathfinding.SetDestination(pickable.RunawayPosition, 5f, 1f, 100, false, false, false, null);
                        else
                        {
                            pickable.IsFinish = false;
                            pickable.StartEatTime = null;
                            pickable.ComponentPathfinding = null;
                        }
                    }
                }
                if (pickable.FlyToPosition.HasValue)
                {
                    Vector3 v2 = pickable.FlyToPosition.Value - pickable.Position;
                    float num7 = v2.LengthSquared();
                    if (num7 >= 0.25f)
                    {
                        pickable.Velocity = 6f * v2 / MathUtils.Sqrt(num7);
                    }
                    else
                    {
                        pickable.AllowToRemove = true;
                        pickable.ToRemove = true;
                        pickable.FlyToPosition = null;
                    }
                }
                else
                {

                    FluidBlock surfaceBlock;
                    float? surfaceHeight;
                    Vector2? vector2 = m_subsystemFluidBlockBehavior.CalculateFlowSpeed(Terrain.ToCell(pickable.Position.X), Terrain.ToCell(pickable.Position.Y + 0.1f), Terrain.ToCell(pickable.Position.Z), out surfaceBlock, out surfaceHeight);
                    if (!pickable.StuckMatrix.HasValue)
                    {
                        TerrainRaycastResult? terrainRaycastResult = m_subsystemTerrain.Raycast(position, vector, useInteractionBoxes: false, skipAirBlocks: true, (int value, float distance) => BlocksManager.Blocks[Terrain.ExtractContents(value)].IsCollidable);
                        if (terrainRaycastResult.HasValue)
                        {
                            int contents = Terrain.ExtractContents(m_subsystemTerrain.Terrain.GetCellValue(terrainRaycastResult.Value.CellFace.X, terrainRaycastResult.Value.CellFace.Y, terrainRaycastResult.Value.CellFace.Z));
                            SubsystemBlockBehavior[] blockBehaviors = m_subsystemBlockBehaviors.GetBlockBehaviors(contents);
                            for (int i = 0; i < blockBehaviors.Length; i++)
                            {
                                blockBehaviors[i].OnHitByProjectile(terrainRaycastResult.Value.CellFace, pickable);
                            }

                            if (m_subsystemTerrain.Raycast(position, position, useInteractionBoxes: false, skipAirBlocks: true, (int value2, float distance) => BlocksManager.Blocks[Terrain.ExtractContents(value2)].IsCollidable).HasValue)
                            {
                                int num8 = Terrain.ToCell(position.X);
                                int num9 = Terrain.ToCell(position.Y);
                                int num10 = Terrain.ToCell(position.Z);
                                int num11 = 0;
                                int num12 = 0;
                                int num13 = 0;
                                int? num14 = null;
                                for (int j = -3; j <= 3; j++)
                                {
                                    for (int k = -3; k <= 3; k++)
                                    {
                                        for (int l = -3; l <= 3; l++)
                                        {
                                            if (!BlocksManager.Blocks[m_subsystemTerrain.Terrain.GetCellContents(j + num8, k + num9, l + num10)].IsCollidable)
                                            {
                                                int num15 = j * j + k * k + l * l;
                                                if (!num14.HasValue || num15 < num14.Value)
                                                {
                                                    num11 = j + num8;
                                                    num12 = k + num9;
                                                    num13 = l + num10;
                                                    num14 = num15;
                                                }
                                            }
                                        }
                                    }
                                }

                                if (num14.HasValue)
                                {
                                    pickable.FlyToPosition = new Vector3(num11, num12, num13) + new Vector3(0.5f);
                                }
                                else
                                {
                                    pickable.ToRemove = true;
                                }
                            }
                            else
                            {
                                Plane plane = terrainRaycastResult.Value.CellFace.CalculatePlane();
                                bool flag2 = vector2.HasValue && vector2.Value != Vector2.Zero;
                                if (plane.Normal.X != 0f)
                                {
                                    float num16 = (flag2 || MathUtils.Sqrt(MathUtils.Sqr(pickable.Velocity.Y) + MathUtils.Sqr(pickable.Velocity.Z)) > 10f) ? 0.95f : 0.25f;
                                    pickable.Velocity *= new Vector3(0f - num16, num16, num16);
                                }

                                if (plane.Normal.Y != 0f)
                                {
                                    float num17 = (flag2 || MathUtils.Sqrt(MathUtils.Sqr(pickable.Velocity.X) + MathUtils.Sqr(pickable.Velocity.Z)) > 10f) ? 0.95f : 0.25f;
                                    pickable.Velocity *= new Vector3(num17, 0f - num17, num17);
                                    if (flag2)
                                    {
                                        pickable.Velocity.Y += 0.1f * plane.Normal.Y;
                                    }
                                }

                                if (plane.Normal.Z != 0f)
                                {
                                    float num18 = (flag2 || MathUtils.Sqrt(MathUtils.Sqr(pickable.Velocity.X) + MathUtils.Sqr(pickable.Velocity.Y)) > 10f) ? 0.95f : 0.25f;
                                    pickable.Velocity *= new Vector3(num18, num18, 0f - num18);
                                }

                                vector = position;
                            }
                        }
                    }
                    else
                    {
                        Vector3 vector3 = pickable.StuckMatrix.Value.Translation + pickable.StuckMatrix.Value.Up * block.ProjectileTipOffset;
                        if (!m_subsystemTerrain.Raycast(vector3, vector3, useInteractionBoxes: false, skipAirBlocks: true, (int value, float distance) => BlocksManager.Blocks[Terrain.ExtractContents(value)].IsCollidable).HasValue)
                        {
                            pickable.Position = pickable.StuckMatrix.Value.Translation;
                            pickable.Velocity = Vector3.Zero;
                            pickable.StuckMatrix = null;
                        }
                    }
                    if (surfaceBlock is WaterBlock && !pickable.SplashGenerated)
                    {
                        m_subsystemParticles.AddParticleSystem(new WaterSplashParticleSystem(m_subsystemTerrain, pickable.Position, large: false));
                        m_subsystemAudio.PlayRandomSound("Audio/Splashes", 1f, m_random.Float(-0.2f, 0.2f), pickable.Position, 6f, autoDelay: true);
                        pickable.SplashGenerated = true;
                        //入水速度为0
                        pickable.Velocity = Vector3.Zero;
                        pickable.Flag = true;

                    }
                    else if (surfaceBlock is MagmaBlock && !pickable.SplashGenerated)
                    {
                        m_subsystemParticles.AddParticleSystem(new MagmaSplashParticleSystem(m_subsystemTerrain, pickable.Position, large: false));
                        m_subsystemAudio.PlayRandomSound("Audio/Sizzles", 1f, m_random.Float(-0.2f, 0.2f), pickable.Position, 3f, autoDelay: true);
                        pickable.SplashGenerated = true;
                        pickable.Flag = true;
                        pickable.Velocity = Vector3.Zero;

                        m_subsystemExplosions.TryExplodeBlock(Terrain.ToCell(pickable.Position.X), Terrain.ToCell(pickable.Position.Y), Terrain.ToCell(pickable.Position.Z), pickable.Value);
                    }
                    else if (surfaceBlock == null)
                    {
                        pickable.SplashGenerated = false;
                    }

                    if (m_subsystemTime.PeriodicGameTimeEvent(1.0, (double)(pickable.GetHashCode() % 100) / 100.0) && (m_subsystemTerrain.Terrain.GetCellContents(Terrain.ToCell(pickable.Position.X), Terrain.ToCell(pickable.Position.Y + 0.1f), Terrain.ToCell(pickable.Position.Z)) == 104 || m_subsystemFireBlockBehavior.IsCellOnFire(Terrain.ToCell(pickable.Position.X), Terrain.ToCell(pickable.Position.Y + 0.1f), Terrain.ToCell(pickable.Position.Z))))
                    {
                        m_subsystemAudio.PlayRandomSound("Audio/Sizzles", 1f, m_random.Float(-0.2f, 0.2f), pickable.Position, 3f, autoDelay: true);
                        pickable.ToRemove = true;
                        m_subsystemExplosions.TryExplodeBlock(Terrain.ToCell(pickable.Position.X), Terrain.ToCell(pickable.Position.Y), Terrain.ToCell(pickable.Position.Z), pickable.Value);
                    }

                    if (!pickable.StuckMatrix.HasValue)
                    {
                        if (vector2.HasValue && surfaceHeight.HasValue && !pickable.Flag)
                        {
                            float num19 = surfaceHeight.Value - pickable.Position.Y;
                            float num20 = MathUtils.Saturate(3f * num19);
                            pickable.Velocity.X += 4f * dt * (vector2.Value.X - pickable.Velocity.X);
                            pickable.Velocity.Y -= 10f * dt;
                            pickable.Velocity.Y += 10f * (1f / block.Density * num20) * dt;
                            pickable.Velocity.Z += 4f * dt * (vector2.Value.Y - pickable.Velocity.Z);
                            pickable.Velocity.Y *= num2;
                        }
                        else if (!pickable.Flag)
                        {
                            pickable.Velocity.Y -= 10f * dt;
                            pickable.Velocity *= num;
                        }
                    }
                }

                pickable.Position = vector;
            }

            foreach (ThrowPickable item in m_pickablesToRemove)
            {
                m_pickables.Remove(item);
                if (this.PickableRemoved != null)
                {
                    this.PickableRemoved(item);
                }
            }

            m_pickablesToRemove.Clear();
        }





    }
}